<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/30 0030
 * Time: 20:06
 */

namespace api\storage\model;


use think\Model;

class FeedDetailModel extends Model
{
    protected $table = 'smt_feed_station_detail';
    protected $uuid = 'uuid';

    /***
     * @param $where
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 根据工单号获取需要物料Id（sub_materiel_id） 对应 所需的总数量（total_count）,以及对应可用的模拟量（simulation_num），还有update_time(用于之后的校验）。
     */
    public function getTotalAndSimulation($where)
    {
        $result = $this->alias('a')
            ->where($where)//条件，一般是传入feed_id
            ->field('a.sub_materiel_id,SUM(a.total_count) as total_count, b.simulation_num,b.update_time')
            ->group('a.sub_materiel_id')//将smt_feed_station_detail表利用sub_materiel_id分类
            ->join(['smt_materiel' => 'b'], 'a.sub_materiel_id = b.sub_materiel_id', 'LEFT')//表smt_feed_station_detail与smt_materiel进行左连接
            ->select();
        return $result;
    }

    /****
     * @param $where
     * @param $field
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取物料细表中符合条件的数据
     *
     */
    public function getFeedDetail($where,$field)
    {
        $result = $this
            ->where($where)
            ->field($field)
            ->order('machine_id , seat_code asc')
            ->select();
        return $result;
    }

    /****
     * @param $where
     * @param $field
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     * 获取物料细表中符合条件的一条数据
     * $where 条件
     * $field 限定字段
     *（其实上面那个也可以实现，只是用find能优化效率，只是不确定效果好不好。）
     */
    public function findFeedDetail($where,$field = '*')
    {
        $result = $this
            ->where($where)
            ->field($field)
            ->find();
        return $result;
    }

    /***
     * @param $where
     * @param $update
     * @return FeedDetailModel
     * 更新料站细表
     */
    public function updateFeedDetail($where,$update){
        $result = $this
            ->where($where)
            ->update($update);
        return $result;
    }

    /***
     * @param $feedUuid 料站表Id
     * @param $machineUuid
     * @param $seatCode 机器站位号
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\Exception 根据这三个条件找出唯一的存在的FeedDetail记录
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function findFeedDetailBy3Condition($feedUuid,$machineUuid,$seatCode){
        $where['f.uuid'] = $feedUuid;
        $where['m.uuid'] = $machineUuid;
        $where['fd.seat_code'] = $seatCode;
        $result = $this->alias('fd')
            ->join(['smt_feed_station' => 'f'],'fd.feed_id = f.feed_id')
            ->join(['smt_machine' => 'm'],'m.machine_id = fd.machine_id')
            ->where($where)
            ->field('fd.*')
            ->find();
        return $result;
    }

    /***
     * @param $feedId
     * @return int|string
     * @throws \think\Exception
     * 检查feedDetail 是否已经全部完成
     */
    public function checkFeedStatus($feedId){
        $where['feed_id'] = $feedId;
        $where['status'] = 1;
        return $this->where($where)->count();
    }

    /***
     * 确认首盘物料所需的数量
     * @param $where
     * @return int|string
     * @throws \think\Exception
     */
    public function getMaterielNumber($where){
        $result = $this->where($where)->field('count(*) count,sub_materiel_id')->group('sub_materiel_id')->select();
        return $result;
    }


}